<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="日志内容" prop="msg">
        <el-input
          v-model="queryParams.msg"
          placeholder="请输入日志内容"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="记录时间" prop="createTime">
        <el-date-picker
          v-model="sj"
          @change="selectTime"
          value-format="yyyy-MM-dd"
          type="datetimerange"
          range-separator="至"
          start-placeholder="记录时间"
          end-placeholder="结束时间"
          :picker-options="pickerOptions"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="操作人" prop="createId">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入操作人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" border :data="ywdwgdLogList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" type="index" width="55" align="center" />
      <el-table-column label="日志内容" align="left" prop="msg">
        <template slot-scope="scope">
          <span style="color: #00afff;cursor: pointer" @click="handleUpdate(scope.row)">{{scope.row.msg}}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="type" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.type === 'add'">新增</span>
          <span v-else-if="scope.row.type === 'update'">修改</span>
          <span v-else-if="scope.row.type === 'xcap'">下场安排</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="createBy" width="100" />
      <el-table-column label="操作时间" align="center" prop="createTime" width="200" ></el-table-column>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100" >-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-view"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--          >查看</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改业务单完工单日志对话框 -->
    <el-dialog v-if="open" :title="title" :visible.sync="open" width="80%" :fullscreen="true" append-to-body>
      <div v-if="form.type === 'add'">
          <info :form="form.fontForm" :title="'新增记录'" :num="3"></info>
      </div>
      <div v-else class="info-main">
        <div class="info-left">
          <info :form="form.fontForm" :title="'修改前记录'" :num="2"></info>
        </div>
        <div class="info-right">
          <info :form="form.laterForm" :title="'修改后记录'" :differentKeys="differentKeys" :num="2"></info>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listYwdwgdLog, getYwdwgdLog, delYwdwgdLog, addYwdwgdLog, updateYwdwgdLog } from "@/api/gd/ywdwgdLog";
import info from './info.vue'
export default {
  name: "YwdwgdLog",
  components: { info },
  data() {
    return {
      differentKeys: [],
      sj: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 业务单完工单日志表格数据
      ywdwgdLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        msg: undefined,
        type: undefined,
        font: undefined,
        later: undefined,
        createBy: undefined,
        params: {
          startTime: undefined,
          endTime: undefined
        }
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    selectTime(time) {
      this.queryParams.params.startTime = time[0]
      this.queryParams.params.endTime = time[1]
    },
    /** 查询业务单完工单日志列表 */
    getList() {
      this.loading = true;
      listYwdwgdLog(this.queryParams).then(response => {
        this.ywdwgdLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        msg: undefined,
        type: undefined,
        font: undefined,
        later: undefined,
        createTime: undefined,
        createBy: undefined,
        createId: undefined,
        delFlag: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams= {
          pageNum: 1,
          pageSize: 10,
          msg: undefined,
          type: undefined,
          font: undefined,
          later: undefined,
          createBy: undefined,
          params: {
          startTime: undefined,
            endTime: undefined
        }
      }
      this.sj = []
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加业务单完工单日志";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getYwdwgdLog(id).then(response => {
        this.loading = false;
        this.form = response.data;

        // 对比不同的数据的key
        let fontForm = this.form.fontForm
        let laterForm = this.form.laterForm
        this.differentKeys = [];
        if (fontForm && laterForm) {
          for (let key in fontForm) {
            if (fontForm[key] !== laterForm[key]) {
              this.differentKeys.push(key);
            }
          }
        }
        this.open = true;
        this.title = "详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateYwdwgdLog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addYwdwgdLog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除业务单完工单日志编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delYwdwgdLog(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gd/ywdwgdLog/export', {
        ...this.queryParams
      }, `ywdwgdLog_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.info-main {
  width: 100%;
  display: flex;
}
.info-left {
  width: 50%;
  padding: 5px;
}
.info-right {
  width: 50%;
  padding: 5px;
}

</style>
